using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using Jeffrey.XFramework;
using Jeffrey.XFramework.XCrawl;

namespace Jeffrey.XProcess
{
    /*
     * use attribute to add some other parameters.
     * such as c_BasePath
     */
    public class StoreToDisk : Storage
    {
        private readonly String s_BasePath = @"C:\Xuthus";

        public StoreToDisk(String name, String description, String classname, String path)
            : this(name, description, classname, path, null) { }
        public StoreToDisk(String name, String description, String classname, String path, Filter filter)
            : base(name, description, classname, path, filter) 
        {
            if (s_BasePath.EndsWith(@"\"))
                m_BasePath = s_BasePath;
            else
                m_BasePath = s_BasePath + @"\";
        }

        protected override void Store(CrawlUri curi)
        {
            if (curi.Status != CrawlUri.StatusCode.S_Succeed || curi.Content == null || curi.Content == String.Empty)
                return;

            String path     = getPathFromUri(curi);
            StreamWriter sw = null;
            try
            {
                if (File.Exists(path))
                    return;

                DirectoryInfo di = new DirectoryInfo(System.IO.Path.GetDirectoryName(path));

                if (!di.Exists)
                    di.Create();

                FileStream fs = File.Create(path);
                sw = new StreamWriter(fs, curi.ContentEncoding);

                sw.Write(curi.Content);

                curi.Content = null;
            }
            catch(Exception e)
            {
                //log this error.
                Logger.LogError(e, "occurred in " + curi.AbsoluteUri + "when Storing...");
            }
            finally
            {
                if (sw != null)
                {
                    sw.Flush();
                    //close StreamWriter Object and BaseStream, FileStream.
                    sw.Close();
                }
            }
        }

        private String getPathFromUri(CrawlUri curi)
        {
            String path = String.Empty;

            String AbsolutePath = curi.AbsolutePath.Replace('/','\\');
            Int32  dot = AbsolutePath.IndexOf('.');

            if (dot == -1)
            {
                if (!AbsolutePath.EndsWith(@"\"))
                    AbsolutePath += @"\";

                // 'text/html' may help
                path = m_BasePath + curi.Host + AbsolutePath + "index.html";
            }
            else
            {
                //at this time do not change the suffix of the file to html.
                path = m_BasePath + curi.Host + AbsolutePath;
            }

            return path;
        }

        private String m_BasePath;
    }
}
